package Model.Draw.Entities;

import Model.Core.IEntity;
import Model.Core.Impl.ActorEntity;
import Model.Draw.Coordinate;
import Model.Draw.Entities.Impl.EntityJoint;
import Model.Draw.ICommunicatorDraw;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Ellipse2D;

/**
 * TCC Project
 * @author jose.luz
 */
public class ActorDraw implements ICommunicatorDraw {

    private ActorEntity actorEntity;
    private EntityJoint center;
    private double size;
    private Ellipse2D circleActor;
    private boolean selected;

    public EntityJoint getCenter() {
        return center;
    }

    public ActorDraw() {
        selected = false;
    }

    public void setCircle(EntityJoint center, double size) {
        this.center = center;
        this.size = size;
    }

    public Color getColor() {
        return selected ? Color.RED : Color.BLACK;
    }

    @Override
    public EntityJoint findNearstJointOf(ICommunicatorDraw otherDraw) {
        return center;
    }

    @Override
    public void setEntity(IEntity entity) {
        this.actorEntity = (ActorEntity) entity;
    }

    @Override
    public IEntity getEntity() {
        return actorEntity;
    }

    @Override
    public void draw(Graphics g) {
        circleActor = new Ellipse2D.Double(center.getX().getCoordinate() - size, center.getY().getCoordinate() - size, size * 2, size * 2);
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setColor(getColor());
        g2.setStroke(new BasicStroke(2));
        g2.draw(circleActor);
        g.fillOval((int) center.getX().getCoordinate() - 1, (int) center.getY().getCoordinate() - 1, 5, 5);
        Font mFont = g.getFont();
        double sizeName = mFont.getStringBounds(actorEntity.getDescription(), ((Graphics2D) g).getFontRenderContext()).getWidth();
        g2.drawString(actorEntity.getDescription(), (int) (center.getX().getCoordinate() - sizeName / 2), (int) (center.getY().getCoordinate() + size + 20));
    }

    @Override
    public boolean contains(int x, int y) {
        return circleActor.contains(x, y);
    }

    @Override
    public void select() {
        selected = true;
    }

    @Override
    public void unselect() {
        selected = false;
    }

    @Override
    public void move(int diffX, int diffY) {
        center.setX(new Coordinate(diffX));
        center.setY(new Coordinate(diffY));
    }

    @Override
    public void rename(String newName) {
        actorEntity.setDescription(newName);
    }
}
